package com.coupon.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;

@Component
public class ScheduledTasks {

    @Autowired
    private TaskScheduler taskScheduler;

    public void scheduleTask(LocalDateTime runAt, Runnable task) {
        // 计算计划执行时间和当前时间的差值
        Duration duration = Duration.between(LocalDateTime.now(), runAt);
        // 安排任务
        taskScheduler.schedule(task, Instant.now().plus(duration));
    }
}